package com.jc.oms.controller.attributevalue;


import com.jc.common.util.WebUtils;
import com.jc.service.attributevalue.AttributeValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("attributeValue")
public class AttributeValueController {

    @Autowired
    private AttributeValueService attributeValueService;


    /**
     * getAttributeName
     */
    @PostMapping("getAttributeName")
    public Map<String, Object> getAttributeName(HttpServletRequest request) {

        Map<String, Object> result = new HashMap<>();

        Map<String, Object> map = this.attributeValueService.queryAllName();

        if (map == null) {
            result.put("code", "4000");
            result.put("msg", "暂时没有数据");
            return result;
        }
        return map;

    }


    /**
     * getAttributeValue
     */
    @PostMapping("getAttributeValue")
    public Map<String, Object> getAttributeValue(HttpServletRequest request) {

        Map<String, Object> result = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        Map<String, Object> map = new HashMap<>();
        try {
            params = WebUtils.ReadAsChars(request);
            if (params == null) {
                result.put("code", "4000");
                result.put("msg", "没有接受到参数~~");
                return result;
            }
            map = this.attributeValueService.getAttributeValue(params);
            if (map == null) {
                result.put("code", "4000");
                result.put("msg", "暂时没有数据");
                return result;
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return map;

    }


    /**
     * 新增规格参数
     */
    @PostMapping("addAttribute")
    public Map<String, Object> addAttribute(HttpServletRequest request) {

        Map<String, Object> result = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        Map<String, Object> map = new HashMap<>();

        try {
            params = WebUtils.ReadAsChars(request);
            if (params == null) {
                result.put("code", "4000");
                result.put("msg", "没有接收到数据");
            }
            map = this.attributeValueService.addAttribute(params);
            if (map != null) {

                return map;
            } else {
                result.put("code", "4000");
                result.put("msg", "保存失败");
            }
        } catch (UnsupportedEncodingException e) {
            result.put("code", "4000");
            result.put("msg", "系统异常,保存失败");
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 删除规格参数
     */
    @PostMapping("delAttribute")
    public Map<String, Object> delAttribute(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        try {
            params = WebUtils.ReadAsChars(request);
            result = this.attributeValueService.delAttribute(params);
            if (result == null) {
                result.put("code", "4000");
                result.put("msg", "删除失败！！");
            }
        } catch (UnsupportedEncodingException e) {
            result.put("code", "4000");
            result.put("msg", "系统异常！！");
            e.printStackTrace();
        }
        return result;
    }

}
